/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.rs.service.impl;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import com.xy.biz.rs.domain.converter.EquipmentConverter;
import com.xy.biz.rs.domain.req.SaveEquipmentRequest;
import com.xy.lang.domain.Result;
import com.xy.tool.context.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xy.biz.rs.domain.bo.EquipmentBO;
import com.xy.biz.rs.domain.query.EquipmentQuery;
import com.xy.biz.rs.service.EquipmentService;
import com.xy.biz.rs.manager.EquipmentManager;

/**
 *
 *
 * @author yzh
 * @date 2021-4-13
 */
@Service
public class EquipmentServiceImpl implements EquipmentService {

	@Autowired
	private EquipmentManager equipmentManager;

	@Override
	public Result<?> saveEquipment(SaveEquipmentRequest request) {
		//判断编号是否重复
		int count = equipmentManager.countEquipment(EquipmentQuery.builder().equipmentCode(request.getEquipmentCode()).build());
		if (count > 0) {
			return Result.ofErrorData(Result.CONFLICT,"已存在的仪器编码");
		}
		EquipmentBO equipmentBO = EquipmentConverter.toBO(request);
		//数据完善
		buildEquipment(equipmentBO);
		int affectedRows = equipmentManager.saveEquipment(equipmentBO);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	private void buildEquipment(EquipmentBO equipmentBO){
		LocalDateTime now = LocalDateTime.now();
		equipmentBO.setCreateTime(now);
		equipmentBO.setUpdateTime(now);
		equipmentBO.setTenantId(UserContextHolder.getTenantId());
		equipmentBO.setDeleteFlag(false);
		equipmentBO.setStatus(1);
	}

	@Override
	public Result<?> saveAllEquipment(Collection<EquipmentBO> collection) {
		int affectedRows = equipmentManager.saveAllEquipment(collection);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> updateEquipment(SaveEquipmentRequest request) {
		EquipmentBO equipmentBO = EquipmentConverter.toBO(request);
		int affectedRows = equipmentManager.updateEquipment(equipmentBO);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> updateEquipmentStatus(SaveEquipmentRequest request) {
		EquipmentBO equipmentBO = EquipmentConverter.toBO(request);
		int affectedRows = equipmentManager.updateEquipment(equipmentBO);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public Result<?> deleteEquipment(Long equipmentId) {
		int affectedRows = equipmentManager.deleteEquipment(equipmentId);
		if (affectedRows > 0) {
			return Result.success();
		}
		return Result.ofErrorRowsAffected();
	}

	@Override
	public EquipmentBO getEquipment(Long equipmentId) {
		return equipmentManager.getEquipment(equipmentId);
	}

	@Override
	public List<EquipmentBO> listEquipmentById(Collection<Long> collection) {
		return equipmentManager.listEquipmentById(collection);
	}

	@Override
	public List<EquipmentBO> listEquipment(EquipmentQuery query) {
		return equipmentManager.listEquipment(query);
	}

	@Override
	public int countEquipment(EquipmentQuery query) {
		return equipmentManager.countEquipment(query);
	}

}
